package com.example.exampleproject.net;

import com.example.exampleproject.App;
import com.example.exampleproject.config.UrlConfig;
import com.google.gson.Gson;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import javax.inject.Inject;
import javax.inject.Singleton;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Singleton
public class ApiService {

    @Inject
    Gson gson;

    @Inject
    ApiService() {
    }

    public ApiDefinition getService() {

        System.out.println(gson);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new CacheInterceptor(App.getApp()))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(ApiDefinition.class);
    }

}
